package cn.rkylin.oms.sysOrder.service;

import cn.rkylin.oms.common.base.AbstractAdapterFactory;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.sysOrder.dao.ISysOrderDAO;
import cn.rkylin.oms.sysOrder.domain.SysOrderGoods;
import cn.rkylin.oms.sysOrder.vo.SysOrderVO;
import cn.rkylin.oms.system.project.vo.ProjectVO;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 系统订单服务层实现
 *
 * @author wangxing
 * @version 1.0
 * @created 2017.09.14
 */
@Service("sysOrderService")
public class SysOrderServiceImpl extends AbstractAdapterFactory implements ISysOrderService {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private ISysOrderDAO sysOrderDAO;

    @Autowired
    private CreateSysOrderService createSysOrderService;// 创建系统订单

    @Autowired
    private CombineSysOrderService combineSysOrderService;// 合并系统订单

    @Autowired
    private SysOrderPromotionRuleService SysOrderPromotionRuleService;// 系统订单促销上赠品

    @Autowired
    private UpdateSysOrderService updateSysOrderService;// 更新系统订单

    @Autowired
    private VerifySysOrderService verifySysOrderService;// 审核系统订单

    /**
     * 日志类
     */
    @Autowired
    private LogUtil logUtil;

    public SysOrderServiceImpl() {

    }

    /**
     * 生成系统订单(1、订单进系统；2、订单合并；3、计算促销上赠品；4、订单更新)
     *
     * @param shopId    店铺id
     * @param buyerNick 买家昵称 客户ID（手动下载用）
     * @param tradeCode 平台交易号（手动下载用）
     * @return
     * @throws Exception
     */
    public int generateSysOrder(String shopId, String buyerNick, String tradeCode) throws Exception {
        int iRes = 0;
//        logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "系统订单自动下载任务service开始---------------------------------------"));
        try {
            // 获取网店基本信息
            ShopVO shopVO = getShop(shopId);
            if (shopVO == null) {
                logUtil.error(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "shopVO is null"));
                return -1;
            }
            logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "获取网店信息结束shopAccount:" + shopVO.getShopAccount()));
            // 项目Id
            String strPrjId = shopVO.getPrjId();
            // 是否允许自动合并订单
            String strIsAutoMergeOrder = shopVO.getIsAutoMergeOrder();
            // 获取项目基本信息
            ProjectVO projectVO = getProject(strPrjId);
            if (projectVO == null) {
                logUtil.error(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "projectVO is null"));
                return -3;
            }
            logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "获取项目信息结束"));
            // 项目简称(用于生成系统订单编号)
            shopVO.setPrjShortName(projectVO.getPrjShortName());
            // --------------------------------------------------------------------------------------------
            // 1、订单进系统
            // begin--------------------------------------------------------------------------
            // 创建系统订单处理
            createSysOrderService.createSysOrder(shopVO, buyerNick, tradeCode);
            // 1、订单进系统
            // end----------------------------------------------------------------------------

            // 2、订单合并
            // begin--------------------------------------------------------------------------
            // 网店开启合并订单开关
            if ("y".equals(strIsAutoMergeOrder)) {
                // 合并系统订单处理
                combineSysOrderService.combineSysOrder(shopVO, buyerNick, tradeCode);
            }
            // 2、订单合并
            // end----------------------------------------------------------------------------

            // 3、订单促销上赠品 begin------------------------------------------
            // 系统订单促销上赠品处理
            SysOrderPromotionRuleService.sysOrderPromotionRule(shopVO, buyerNick, tradeCode);
            // 3、订单促销上赠品 end--------------------------------------------

            // ４、订单平台状态更新 begin------------------------------------------
            // 更新系统订单处理 1：更新非合并和合并前订单平台状态和卖家备注；
            // 2：更新非合并订单系统完成状态；
            // 3：更新合并后订单系统完成状态(子订单平台交易状态都已经是 交易完成或交易取消)；
            // 4：更新合并后订单卖家备注为合并前订单的累加
            updateSysOrderService.updateSysOrder(shopVO, buyerNick, tradeCode);
            // ４、订单平台状态更新 end--------------------------------------------
            iRes = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            iRes = 0;
            logUtil.error(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "系统订单自动下载任务service层异常：" + ex.getMessage()), ex);
        }
//        logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "系统订单自动下载任务service结束---------------------------------------"));
        return iRes;
    }

    /**
     * 自动审核分派
     *
     * @param shopId 店铺id
     */
    public int verifyAndAssignOrder(String shopId) throws Exception {
        int iRes = 0;
//        logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "系统订单自动审核分派任务service开始---------------------------------------"));
        try {
            // 获取网店基本信息
            ShopVO shopVO = getShop(shopId);
            if (shopVO == null) {
                return -1;
            }
            logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "获取网店信息结束"));
            // 项目Id
            String strPrjId = shopVO.getPrjId();
            // 是否允许自动审核订单
            String strIsAutoAuthen = shopVO.getIsAutoAuthen();
            // 获取项目基本信息
            ProjectVO projectVO = getProject(strPrjId);
            if (projectVO == null) {
                return -3;
            }
            logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "获取项目信息结束"));
            // 项目简称(用于生成系统发货单编号)
            shopVO.setPrjShortName(projectVO.getPrjShortName());
            // --------------------------------------------------------------------------------------------
            // 店铺开启自动审核订单开关
            if ("y".equals(strIsAutoAuthen)) {
                // 进入订单审核分派处理
                verifySysOrderService.verifySysOrder(shopVO);
            }
            iRes = 1;
        } catch (Exception ex) {
            iRes = 0;
        }
//        logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "系统订单自动审核分派任务service结束---------------------------------------"));
        return iRes;
    }

    /**
     * 获取系统订单信息.
     *
     * @throws Exception
     */
    public List<SysOrderVO> getSysOrderList(Map<String, String> sysOrderMap) throws Exception {
        return sysOrderDAO.selectSysOrderList(sysOrderMap);
    }

    /**
     * 锁定订单.
     *
     * @see cn.rkylin.oms.sysOrder.service.ISysOrderService#updateLockOrder(cn.rkylin.oms.sysOrder.vo.SysOrderVO)
     */
    @Override
    public int updateLockOrder(SysOrderVO sysOrderVO) throws Exception {
        return sysOrderDAO.updateByPrimaryKeySelectiveSysOrder(sysOrderVO);
    }


    /**
     * 更新系统订单信息
     *
     * @param sysOrderVO
     * @return
     * @throws Exception
     */
    public int updateByPrimaryKeySelectiveSysOrder(SysOrderVO sysOrderVO) throws Exception {
        return sysOrderDAO.updateByPrimaryKeySelectiveSysOrder(sysOrderVO);
    }

    /**
     * 更新系统订单平台发货时间
     *
     * @return
     * @author jipengcheng
     */
    public int updateByTradeCodeSysOrderConsignTime(SysOrderVO sysOrderVO) throws Exception {
        return sysOrderDAO.updateByTradeCodeSysOrderConsignTime(sysOrderVO);
    }

    /**
     * 更新系统订单商品明细
     *
     * @param record
     * @return
     * @throws Exception
     */
    public int updateByPrimaryKeySelectiveSysOrderGoods(SysOrderGoods record) throws Exception {
        return sysOrderDAO.updateByPrimaryKeySelectiveSysOrderGoods(record);
    }

}